package com.lanou.renrenxin.adapter;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ListView;

/**
 * Created by zy on 16/3/3.
 */
public class SlideIndex extends View {
    private  static final String[] WORDS = {"A","B","C","D","F","G","H",
            "I","j","K","L","M","N","O","P","Q","R",
            "S","T","U","V","W","X","Y","Z"};
    private int preHeight = 18;//每一个字母的高度
    private ContackListAdapter contackListAdapter;
    private ListView mlistView;//需要设置索引的Listview
    private Paint mpaint;//画笔
    private int blankHeight;//空白的高度,留着计算
//    private MyBaseAdapterPersonAll myBaseAdapterPersonAll;
    private float height;

    //设置ListView
    public void setMlistView(ListView mlistView) {
        this.mlistView = mlistView;
        contackListAdapter = (ContackListAdapter) mlistView.getAdapter();
        ;//获得ListView关联的Adapter
    }



    public SlideIndex(Context context) {
        super(context);
        init();
    }

    public SlideIndex(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    //初始化画笔方法
    private void init(){
        mpaint = new Paint();
        mpaint.setColor(Color.BLACK);
        //文字方式式对其居中
        mpaint.setTextAlign(Paint.Align.CENTER);
        //将高度从dp转换成px
        preHeight = dpToPx(preHeight,getContext());
        //只接受px,设置字体大小
        mpaint.setTextSize(preHeight);
        //抗锯齿
        mpaint.setAntiAlias(true);
    }

    //在该声明周期中会测量控件的宽高
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //获得我们组件的高度
        blankHeight = (getMeasuredHeight() - (WORDS.length * preHeight))/(WORDS.length - 1);
    }


    protected void onDraw(Canvas canvas){
        super.onDraw(canvas);
        float center = getWidth() /2;
        height = getHeight() / WORDS.length;
        for (int i = 0; i < WORDS.length; i++) {
            //再该组件上画文字
            canvas.drawText(WORDS[i],center,(i+1)*height,mpaint);
        }
    }

    //该方法会处理用户的点击事件
    @Override
    public boolean onTouchEvent(MotionEvent event) {

        if (event.getAction()==MotionEvent.ACTION_DOWN){
            //当用户按下的时候
            int index = (int) (event.getY()/(preHeight+blankHeight));
            if (index>=WORDS.length){
                //做一个修正
                index = WORDS.length-1;
            }else if (index<0){
                index = 0;
            }
//            // 通过字母获得ListView的条数,并滚动到该位置
//            int pos = myAdapter.getIndexFromString(WORDS[index]);
//            if (pos>=0){
//                mlistView.setSelection(pos);//ListView滚动到pos位置
//            }

        }
        return super.onTouchEvent(event);
    }

    //将dp转换成px
    private int dpToPx(int dp,Context context){
        int px = 0;
        //获得2屏幕的dpi,需要通过Resources下的DisplayMetrics中的destiyDip
        Resources resources = context.getResources();
        DisplayMetrics metrics = resources.getDisplayMetrics();
        //+0.5是为了四舍五入
        px = (int) (dp * metrics.densityDpi/160 + 0.5f);
        return px;
    }
}
